import socket  
import struct
import time

def buildframe1(frame_str):
    word = frame_str.split(' ')
    frame_list = []
    for i in word:
        if i.strip()!="":
            a=int(i,16)
            frame_list.append(a)
    return frame_list

def buildframe2(frame_str):
    str_len = len(frame_str)
    frame_list = []
    for i in range(str_len/2):
        a=int(frame_str[i+i:i+i+2],16)
        frame_list.append(a)
    return frame_list

def sendframe1(frame_str):
    frame_list = buildframe1(frame_str)
    for byte in frame_list:
        ss.send(struct.pack('B', byte))  

def sendframe2(frame_str):
    frame_list = buildframe2(frame_str)
    for byte in frame_list:
        ss.send(struct.pack('B', byte))

def showbuf(frame):
    frame_len = len(frame)
    frame_format = '%dB' % frame_len
    aa = struct.unpack(frame_format, frame)
    print frame_len, '->', aa
    

print 'start'  
address = ('', 2404)  
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(address)  
s.listen(5)  
  
ss, addr = s.accept()  
print 'got connected from',addr  

  
frame = ss.recv(512)
showbuf(frame)

#sendframe1('68 8C 52 00 02 00 01 FF 14 00 01 00 FB 02 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00')
#sendframe1('68 8D 5E 00 02 00 15 C0 14 00 01 00 41 40 00 E9 03 D0 03 F2 03 22 00 BD 06 BE 06 BD 06 0A 04 0C 04 0A 04 54 02 4B 02 52 02 3E 07 0E 01 7C 00 E6 03 DF 03 EF 03 D6 03 D6 03 DB 03 84 07 C3 01 A2 00 0B 04 0B 04 09 04 9A 02 96 02 99 02 67 07 37 01 7E 00 E8 03 D0 03 F3 03 4D 04 4B 04 46 04 A6 07 01 02 9A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 8D 01 94 01 98 01 96 07 BC 00')


sendframe2('6815220000001E010300001F0100000034A20D0A1F030B')
frame = ss.recv(512)
showbuf(frame)

sendframe2('683424000000098D0100021D0A4000586600206D00206D00F86C00000000100000000000D0FF0018FF00F8FF00000000000000F8FF00')
frame = ss.recv(512)
showbuf(frame)

sendframe2('683426000000098D0100021E0A4000586500286D00306D00186D0000000038000010000028FF00500000000000F8FF00F8FF00F8FF00')
frame = ss.recv(512)
showbuf(frame)

sendframe2('68132800000009820100012B014000500A00A80900')
frame = ss.recv(512)
showbuf(frame)

sendframe2('68762A00000009A30100012B814000480A00480A00500A00684300904100A04100F87200787100587300D80A0040FD00300B00706700E8FF00000000400300A80000900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000')
frame = ss.recv(512)
showbuf(frame)

sendframe2('6835C00000000F882500021A01640000000000010000000002000000000300000000040000000005000000000600000000070000000008')
frame = ss.recv(512)
showbuf(frame)
    
time.sleep(10)  
ss.close()  
s.close()

print 'end'